﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

using OpenTKLightingTool.Data;
using OpenTKLightingTool.Yaml;
using Ignitron.EEngine;

namespace OpenTKLightingTool.Forms
{
    /// <summary>
    /// Interaction logic for LightingToolWindow.xaml
    /// </summary>
    public partial class LightingToolWindow : Window
    {
        private DataStorage mStorage;
        private IEngine mEngine;

        public LightingToolWindow(IEngine engine)
        {
            InitializeComponent();

            mEngine = engine;
            uxRender.Click += uxRender_Click;
            uxExport.Click += uxExport_Click;
            uxImport.Click += uxImport_Click;
        }

        void uxImport_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;
            dlg.DefaultExt = "lights";
            dlg.Filter = "Files (*.lights)|*.lights";
            if (dlg.ShowDialog() == true)
            {
                try
                {
                    ExportImport import = new ExportImport();
                    import.Load(dlg.FileName, mStorage, mEngine);
                    SetStorage(mStorage);
                }
                catch
                {
                    MessageBox.Show("Import failed.", "Error");
                }
            }
        }

        void uxExport_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.CheckPathExists = true;
            dlg.OverwritePrompt = true;
            dlg.DefaultExt = "lights";
            dlg.Filter = "Files (*.lights)|*.lights";
            if (dlg.ShowDialog() == true)
            {
                try
                {
                    ExportImport export = new ExportImport();
                    export.Save(dlg.FileName, mStorage);
                }
                catch
                {
                    MessageBox.Show("Export failed.", "Error");
                }
            }
        }

        void uxRender_Click(object sender, RoutedEventArgs e)
        {
            uxLights.Update();
            uxMaterials.Update();
            uxAxesSystem.Update();
            mStorage.Lighting = uxLighting.GetLighting();

            if (RenderPressedEvent != null)
            {
                RenderPressedEvent(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Sets data
        /// </summary>
        internal void SetStorage(DataStorage storage)
        {
            mStorage = storage;
            uxLights.SetData(storage.Lights);
            uxMaterials.SetData(storage.Materials);
            uxLighting.SetLighting(storage.Lighting);
            uxAxesSystem.SetData(storage.AxesSystem);
        }

        internal event EventHandler RenderPressedEvent;

    }
}
